from django.db import models
from Administrative_office_system.utils.BaseModel import BaseModel, IssueStampFileName
# Create your models here.
from personal_center.models import User
from system_setup.models import *
from back_management.models import *


class ShortMessageApproval(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人'),
        ('two', '科室分管领导'),
        ('three', '医院办公室接收'),
        ('end', '结束'),
        ('refuse', '拒绝'),
        ('stop', '终止')
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", blank=True, null=True, on_delete=models.CASCADE)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='short_message_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='short_message_already_audit_users', blank=True)
    reason = models.TextField(max_length=255, verbose_name='事由', default='')
    content = models.TextField(verbose_name="短信内容", default='')
    recipient = models.TextField(verbose_name="接收人", default='')
    note = models.TextField(verbose_name="备注", default='')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    can_revise = models.CharField('是否可以修改', max_length=50, default='1')

    def get_state_str(self):
        if self.state == "end":
            return '结束'
        elif self.state == 'refuse':
            return '拒绝'
        elif self.state == 'stop':
            return "终止"
        elif self.state == 'draft':
            return "草稿"
        else:
            return '审核中'

class ShortMessageApprovalRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科室负责人'),
        ('two', '科室分管领导'),
        ('three', '医院办公室接收'),
    )
    parent_id = models.ForeignKey('ShortMessageApproval', verbose_name="短信审批", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    node = models.CharField('节点', max_length=10, choices=NODE, default='')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见")

class CommitteeTopics(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '委员会办公室审核'),
        ('two', '副主任委员审核'),
        ('three', '主任委员审核'),
        ('end', '结束'),
        ('refuse', '拒绝'),
        ('stop', '终止'),
        ('unmetting', '待上会'),
        ('metting', '已上会'),
    )
    TYPE = (
        ('1', '医疗类'),
        ('2', '非医疗类')
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", blank=True, null=True, on_delete=models.CASCADE)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='committee_topics_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='committee_topics_already_audit_users', blank=True)
    number = models.CharField('议题编号', max_length=128, blank=True)
    topic_name = models.CharField('议题名称', max_length=255, blank=True, default="")
    help_department = models.ManyToManyField('administrative.Company', related_name='committee_topics_help_department', verbose_name='委员会议题协办科室', blank=True)
    expert_database = models.ForeignKey('ExpertDatabase', verbose_name="专家库", blank=True, null=True, on_delete=models.SET_NULL, default='')
    committee_type = models.CharField('委员会类型', max_length=10, choices=TYPE, default='')
    committee_level = models.CharField('委员会级别', max_length=50, default='')
    committee_name = models.ForeignKey('back_management.CommitteeTable', verbose_name="委员会名称", blank=True, null=True, on_delete=models.SET_NULL, default='')
    topic_description = models.TextField(max_length=500, verbose_name='议题简述')
    policy_basis = models.TextField(max_length=500, verbose_name='政策依据', blank=True)
    proposed_options = models.TextField(max_length=500, verbose_name='拟供选择方案', blank=True)
    note = models.TextField(verbose_name="备注", default='')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    can_revise = models.CharField('是否可以修改', max_length=50, default='1')

    def get_state_str(self):
        if self.state == "end":
            return '结束'
        elif self.state == 'refuse':
            return '拒绝'
        elif self.state == 'stop':
            return "终止"
        elif self.state == 'draft':
            return "草稿"
        elif self.state == 'unmetting':
            return "待上会"
        elif self.state == 'metting':
            return "已上会"
        else:
            return '审核中'

class CommitteeTopicsRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '委员会办公室审核'),
        ('two', '副主任委员审核'),
        ('three', '主任委员审核'),
    )
    parent_id = models.ForeignKey('CommitteeTopics', verbose_name="委员会议题", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    node = models.CharField('节点', max_length=10, choices=NODE, default='')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见")

class ExpertDatabase(BaseModel):
    name = models.CharField('专家库名称', max_length=255, blank=True, default="")
    description = models.CharField('描述', max_length=255, blank=True, default="")

class ExpertPeople(BaseModel):
    name = models.CharField('专家名称', max_length=255, blank=True, default="")
    parent_id = models.ForeignKey('ExpertDatabase', verbose_name="对应专家库名称", blank=False, null=False, on_delete=models.CASCADE)

class FlowModify(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科主任审核'),
        ('two', '分管领导审核'),
        ('three', '医院办公室选择审核人'),
        ('four', '审核人审核'),
        ('five', '网络信息科接收'),
        ('end', '结束'),
        ('refuse', '拒绝'),
        ('stop', '终止')
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", blank=True, null=True, on_delete=models.CASCADE)
    affiliation_model = models.ForeignKey('system_setup.CustomerFormModule', verbose_name="所属模块", blank=True, null=True, on_delete=models.SET_NULL, default="")
    modify_flow = models.ForeignKey('back_management.ProcessNodesMainTable', verbose_name="修改流程", blank=True, null=True, on_delete=models.SET_NULL, default="")
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='flow_modify_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='flow_modify_already_audit_users', blank=True)
    reason = models.CharField('修改原因', max_length=255, blank=True, default="")
    note = models.TextField(verbose_name="备注", default='')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    can_revise = models.CharField('是否可以修改', max_length=50, default='1')
    modification_method = models.CharField('流程类型', max_length=10, blank=True, default="1")          # 1：流程修改， 2：流程新增

    def get_state_str(self):
        if self.state == "end":
            return '结束'
        elif self.state == 'refuse':
            return '拒绝'
        elif self.state == 'stop':
            return "终止"
        elif self.state == 'draft':
            return "草稿"
        else:
            return '审核中'

class FlowModifyRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科主任审核'),
        ('two', '分管领导审核'),
        ('three', '网络信息科接收转审/结束'),
        ('four', '审核人审核'),
        ('five', '网络信息科接收'),
    )
    parent_id = models.ForeignKey('FlowModify', verbose_name="流程节点修改", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    node = models.CharField('节点', max_length=10, choices=NODE, default='')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见")

class DailyWorkSupervision(BaseModel):
    STATE = (
        ('normal', '正常'),
        ('overdue', '超期'),
        ('end', '结束'),
    )
    apply_user = models.ForeignKey('personal_center.User', verbose_name="发起人", related_name="Initiator", blank=True, null=True, on_delete=models.SET_NULL)
    title = models.CharField('标题', max_length=255, blank=True, default="")
    job_description = models.CharField('工作描述', max_length=255, blank=True, default="")
    company = models.ForeignKey('administrative.Company', verbose_name="责任科室", related_name="Responsible_department", blank=True, null=True, on_delete=models.CASCADE)
    start_time = models.DateTimeField(blank=True, null=True, verbose_name='开始时间')
    end_time = models.DateTimeField(blank=True, null=True, verbose_name='结束时间')
    responsible_person = models.ForeignKey('personal_center.User', verbose_name="负责人", related_name="Responsible", blank=True, null=True, on_delete=models.SET_NULL)
    collaboration = models.ForeignKey('administrative.Company', verbose_name="协作科室", related_name="Collaboration_department", blank=True, null=True, on_delete=models.CASCADE)
    note = models.CharField(verbose_name="备注", max_length=255, default='')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    attachment_name = models.CharField('附件名字', max_length=255, blank=True, default="")
    state = models.CharField('状态', max_length=10, choices=STATE, default='normal')

class DailyWorkSupervisionRecord(BaseModel):
    parent_id = models.ForeignKey('DailyWorkSupervision', verbose_name="日常工作督查", blank=False, null=False, on_delete=models.CASCADE)
    node = models.CharField('节点', max_length=10, default='')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="反馈人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="工作反馈")

class WorkSuperviseHandle(BaseModel):
    STATE = (
        ('one', '责任科室填写落实情况'),
        ('two', '责任人分管领导'),
        ('three', '医院办公室转审/结束'),
        ('four', '转审人员审核'),
        ('five', '医院办公室结束流程'),
        ('six', '超时'),
        ('end', '结束'),
        ('stop', '终止')
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    job_description = models.CharField('工作描述', max_length=255, blank=True, default="")
    job_source = models.CharField('工作来源', max_length=155, blank=True, default="")
    company = models.ForeignKey('administrative.Company', verbose_name="责任科室", related_name="supervise_department", blank=True, null=True, on_delete=models.CASCADE)
    responsible_person = models.ForeignKey('personal_center.User', verbose_name="负责人", related_name="supervise_person", blank=True, null=True, on_delete=models.SET_NULL)
    start_time = models.DateTimeField(blank=True, null=True, verbose_name='开始时间')
    end_time = models.DateTimeField(blank=True, null=True, verbose_name='结束时间')
    complete_requirements = models.CharField('完成要求', max_length=255, blank=True, default="")
    note = models.CharField(verbose_name="备注", max_length=255, default='')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    attachment_name = models.CharField('附件名字', max_length=255, blank=True, default="")
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='work_supervise_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='work_supervise_already_audit_users', blank=True)
    transfer_supervision_office = models.CharField(verbose_name="是否转至监察室", max_length=10, default='0')
    old_state = models.CharField('老状态', max_length=10, choices=STATE, default='')

    def get_state_str(self):
        if self.state == "end":
            return '结束'
        elif self.state == 'stop':
            return "终止"
        elif self.state == 'one':
            return "责任科室接收"
        else:
            return "医院办公室"

class WorkSuperviseHandleRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '责任科室填写落实情况'),
        ('two', '责任科室分管院长审批'),
        ('three', '医院办公室(可转审)'),
        ('four', '转审人员审批(可退回、继续流转)'),
        ('five', '医院办公室归档'),
    )
    parent_id = models.ForeignKey('WorkSuperviseHandle', verbose_name="办公室工作督办", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    node = models.CharField('节点', max_length=10, choices=NODE, default='')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见")
    file_path = models.CharField('附件路径', max_length=255, default='')

class OverdueFlow(BaseModel):
    parent_id = models.ForeignKey('WorkSuperviseHandle', verbose_name="办公室工作督办", blank=False, null=False, on_delete=models.CASCADE)
    deadline = models.CharField(verbose_name="是否发起限期告知", max_length=10, default='0')
    process_owner = models.ForeignKey('personal_center.User', verbose_name="流程负责人", blank=True, null=True, on_delete=models.SET_NULL)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='Overdue_flow_audit_users', blank=True)

class DeadlineNotification(BaseModel):
    STATE = (
        ('zero', '责任科室分管领导'),
        ('one', '责任科室'),
        ('two', '监察室'),
        ('end', '结束'),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="发起人", blank=True, null=True, on_delete=models.SET_NULL)
    responsible_person = models.ForeignKey('personal_center.User', verbose_name="负责人", related_name="responsible_person", blank=True, null=True, on_delete=models.SET_NULL)
    work_id = models.ForeignKey('WorkSuperviseHandle', verbose_name="办公室工作督办", blank=False, null=False, on_delete=models.CASCADE)
    overdue_id = models.ForeignKey('OverdueFlow', verbose_name="超期流程", blank=False, null=False, on_delete=models.CASCADE)
    end_deadline_time = models.DateTimeField(blank=True, null=True, verbose_name='限期结束时间')
    initiate_or_not = models.CharField(verbose_name="是否发起问责流程", max_length=10, default='0')
    view_or_not = models.CharField(verbose_name="责任科室分管领导是否查看", max_length=10, default='0')
    leaders_obj = models.ForeignKey('personal_center.User', verbose_name="责任科室分管领导", related_name="leaders_obj_person", blank=True, null=True, on_delete=models.SET_NULL)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='deadline_notification_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='deadline_notification_already_audit_users', blank=True)

class DeadlineNotificationRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '责任科室'),
        ('two', '监察室'),
    )
    parent_id = models.ForeignKey('DeadlineNotification', verbose_name="流程节点修改", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    node = models.CharField('节点', max_length=10, choices=NODE, default='')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见")

class InitiateAccountability(BaseModel):
    STATE = (
        ('one', '监察室分管领导'),
        ('two', '责任科室接收'),
        ('three', '监察室提交问责建议'),
        ('four', '、监察室分管领导'),
        ('five', '、监督执行'),
        ('end', '结束'),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="发起人", blank=True, null=True, on_delete=models.SET_NULL)
    responsible_person = models.ForeignKey('personal_center.User', verbose_name="负责人", related_name="initiate_person", blank=True, null=True, on_delete=models.SET_NULL)
    work_id = models.ForeignKey('WorkSuperviseHandle', verbose_name="办公室工作督办", blank=False, null=False, on_delete=models.CASCADE)
    dead_id = models.ForeignKey('DeadlineNotification', verbose_name="限期整改流程", blank=False, null=True, on_delete=models.CASCADE, default="")
    accountability_recommendations = models.CharField(verbose_name="拟问责建议", max_length=255, default='')
    appeal_end_time = models.DateTimeField(blank=True, null=True, verbose_name='申诉结束时间')
    whether_meeting = models.CharField(verbose_name="是否上会", max_length=10, default='')
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='initiate_accountability_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='initiate_accountability_already_audit_users', blank=True)
    whether_appeal = models.CharField(verbose_name="是否申诉", max_length=10, default='')

    def get_str_state(self):
        if self.state == 'end':
            return '结束'
        else:
            return '审核中'

class InitiateAccountabilityRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '监察室分管领导'),
        ('two', '责任科室接收'),
        ('three', '监察室提交问责建议'),
        ('four', '、监察室分管领导'),
        ('five', '、监督执行'),
    )
    parent_id = models.ForeignKey('InitiateAccountability', verbose_name="问责程序", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    node = models.CharField('节点', max_length=10, choices=NODE, default='')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", default="")

class AttendedMeeting(BaseModel):
    STATE = (
        ('voting', '投票中'),
        ('adopt', '同意'),
        ('suspension', '暂缓'),
        ('nopass', '未通过'),
        ('notstart', '未开始'),
        ('ending', '结束'),
    )
    STATUS = (
        ('progress', '进行中'),
        ('ending', '完结'),
    )
    topic = models.ForeignKey('CommitteeTopics', verbose_name="委员会议题", blank=True, null=True, on_delete=models.SET_NULL)
    state = models.CharField('状态', max_length=10, choices=STATE, default='notstart')
    committee_name = models.ForeignKey('back_management.CommitteeTable', verbose_name="委员会名称", related_name="AttendedMeetingCommitteeTable", blank=True, null=True, on_delete=models.SET_NULL, default='')
    meet_time = models.DateTimeField(verbose_name='会议时间', default=timezone.now)
    status = models.CharField('会议进程', max_length=10, choices=STATUS, default='progress')
    number = models.CharField('会议次数', max_length=20, default='1')
    topic_year = models.CharField('会议年份', max_length=20, default=f"{datetime.now().year}")
    compere = models.ForeignKey('personal_center.User', verbose_name="主持人", blank=False, null=True, on_delete=models.CASCADE, default="")

# 参会领导
class AttendedMeetingPeople(BaseModel):
    STATE = (
        ('voting', '未投票'),
        ('agree', '同意'),
        ('opposition', '反对'),
        ('waiver', '弃权'),
    )
    leaders = models.CharField('投票人', max_length=50, default="")
    topics_id = models.ForeignKey(
        'AttendedMeeting',
        verbose_name="投票议题",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    node = models.TextField("意见", default='')
    state = models.CharField('状态', max_length=10, choices=STATE, default='voting')
    host = models.CharField('是否主持', max_length=10, default=True)

class CommitteeAttendance(BaseModel):
    committee_name = models.ForeignKey('back_management.CommitteeTable', verbose_name="委员会名称", related_name="CommitteeAttendanceName", blank=True, null=True, on_delete=models.SET_NULL, default='')
    number = models.CharField('会议次数', max_length=20, default='1')
    signee = models.CharField('签到人', max_length=50, default='')
    sing = models.CharField('是否签到', max_length=10, default='0')
    uid = models.CharField(max_length=128, verbose_name='扫码设备', default='')

class CommitteeMinutes(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '委员会办公室'),
        ('two', '会签'),
        ('three', '签发'),
        ('four', '分发'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    drafter = models.CharField(max_length=50, verbose_name='拟稿人')
    drafter_company = models.CharField(max_length=128, verbose_name='拟稿人科室')
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='committee_memo_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='committee_memo_already_audit_users', blank=True)
    document_number = models.CharField(max_length=30, verbose_name='字号/文号')
    document_year = models.CharField(max_length=30, verbose_name='公文年份', default='2024')
    main_company = models.CharField(max_length=100, verbose_name='主送', blank=True)
    vice_company = models.CharField(max_length=100, verbose_name='抄送', blank=True)
    servings = models.CharField(max_length=20, verbose_name='份数', default='')
    conference_memo_text = models.TextField(verbose_name='事由')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    can_revise = models.CharField('是否可以修改', max_length=50, default='1')

    def get_state_str(self):
        if self.state == 'four':
            return '待分发'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'

class CommitteeMinutesRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '委员会办公室'),
        ('two', '会签'),
        ('three', '签发')
    )
    parent_id = models.ForeignKey('CommitteeMinutes', verbose_name="委员会会议纪要", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

class CommitteeMinutesPeople(BaseModel):
    STATE = (
        ('accept', '未读'),
        ('accepted', '已读'),
    )
    parent_id = models.ForeignKey('CommitteeMinutes', verbose_name="委员会纪要", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='accept')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="人员", blank=False, null=False, on_delete=models.CASCADE)

# 绩效 (正01表)
class PerformanceAllocation(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_name = models.CharField(max_length=100, verbose_name='科室名称', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    department_director = models.CharField(max_length=100, verbose_name='科室负责人', blank=True, default="")
    job_number = models.CharField(max_length=100, verbose_name='工号', blank=True, default="")
    workload = models.CharField(max_length=100, verbose_name='工作量绩效', blank=True, default="")
    synthesize = models.CharField(max_length=100, verbose_name='综合考核绩效', blank=True, default="")
    department_total = models.CharField(max_length=100, verbose_name='科室绩效合计', blank=True, default="")
    director_performance = models.CharField(max_length=100, verbose_name='科主任科室绩效', blank=True, default="")
    director_control_performance = models.CharField(max_length=100, verbose_name='科主任管理绩效', blank=True, default="")
    night_shift_performance = models.CharField(max_length=100, verbose_name='夜班绩效', blank=True, default="")
    consultation_performance = models.CharField(max_length=100, verbose_name='会诊绩效', blank=True, default="")
    outpatient_special_performance = models.CharField(max_length=100, verbose_name='门诊专项绩效', blank=True, default="")
    medical_community = models.CharField(max_length=100, verbose_name='医共体绩效', blank=True, default="")
    other_single = models.CharField(max_length=100, verbose_name='其他单项绩效', blank=True, default="")
    talent_reserve = models.CharField(max_length=100, verbose_name='人才储备绩效', blank=True, default="")
    special_performance = models.CharField(max_length=100, verbose_name='专项绩效合计', blank=True, default="")
    expected_performance = models.CharField(max_length=100, verbose_name='应发绩效总计', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 临床科室
class PerformanceAllocationClinical(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_name = models.CharField(max_length=100, verbose_name='科室名称', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    department_director = models.CharField(max_length=100, verbose_name='科室负责人', blank=True, default="")
    workload = models.CharField(max_length=100, verbose_name='工作量绩效', blank=True, default="")
    new_business = models.CharField(max_length=100, verbose_name='新技术/新业务绩效', blank=True, default="")
    unit_checkup = models.CharField(max_length=100, verbose_name='单位体检绩效', blank=True, default="")
    apoplexy_stethalgia = models.CharField(max_length=100, verbose_name='卒中/胸痛绩效', blank=True, default="")
    prevention_control = models.CharField(max_length=100, verbose_name='疫情防控', blank=True, default="")
    college_control = models.CharField(max_length=100, verbose_name='高考体检', blank=True, default="")
    talent_reserve = models.CharField(max_length=100, verbose_name='新入职人员人才储备绩效', blank=True, default="")
    nursing_documents = models.CharField(max_length=100, verbose_name='护理文书奖惩', blank=True, default="")
    departmental_disputes = models.CharField(max_length=100, verbose_name='科室/纠纷跑账', blank=True, default="")
    department_total = models.CharField(max_length=100, verbose_name='科室绩效合计', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 人才储备绩效
class PerformanceAllocationTalentReserve(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_name = models.CharField(max_length=100, verbose_name='科室名称', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    department_director = models.CharField(max_length=100, verbose_name='科室负责人', blank=True, default="")
    name = models.CharField(max_length=100, verbose_name='姓名', blank=True, default="")
    title_level = models.CharField(max_length=100, verbose_name='职称级别', blank=True, default="")
    technical_title = models.CharField(max_length=100, verbose_name='技术职称', blank=True, default="")
    distribution_standards = models.CharField(max_length=255, verbose_name='发放标准（元）', blank=True, default="")
    due_this_month = models.CharField(max_length=100, verbose_name='本月应发（元）', blank=True, default="")
    leave_days = models.CharField(max_length=100, verbose_name='请假天数', blank=True, default="")
    start_time = models.CharField(max_length=100, verbose_name='开始发放时间', blank=True, default="")
    end_time = models.CharField(max_length=100, verbose_name='终止时间', blank=True, default="")
    node = models.CharField(max_length=255, verbose_name='备注', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 其他单项
class PerformanceAllocationOtherSingleItems(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_name = models.CharField(max_length=100, verbose_name='科室名称', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    job_number = models.CharField(max_length=100, verbose_name='工号', blank=True, default="")
    name = models.CharField(max_length=100, verbose_name='姓名', blank=True, default="")
    news_day = models.CharField(max_length=100, verbose_name='元旦', blank=True, default="")
    spring_festival = models.CharField(max_length=100, verbose_name='春节', blank=True, default="")
    total = models.CharField(max_length=100, verbose_name='合计', blank=True, default="")
    days = models.CharField(max_length=100, verbose_name='天数', blank=True, default="")
    money = models.CharField(max_length=100, verbose_name='金额', blank=True, default="")
    node = models.CharField(max_length=255, verbose_name='备注', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 医共体专项
class PerformanceAllocationMedicalCommunity(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_name = models.CharField(max_length=100, verbose_name='科室名称', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    total = models.CharField(max_length=100, verbose_name='合计', blank=True, default="")
    node = models.CharField(max_length=255, verbose_name='备注', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 门诊专项绩效
class PerformanceAllocationOutpatientSpecialty(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_name = models.CharField(max_length=100, verbose_name='科室名称', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    job_number = models.CharField(max_length=100, verbose_name='工号', blank=True, default="")
    doctor = models.CharField(max_length=100, verbose_name='医生', blank=True, default="")
    person_time = models.CharField(max_length=100, verbose_name='总人次', blank=True, default="")
    money = models.CharField(max_length=100, verbose_name='金额', blank=True, default="")
    total_performance = models.CharField(max_length=100, verbose_name='绩效合计', blank=True, default="")
    node = models.CharField(max_length=255, verbose_name='备注', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 会诊绩效
class PerformanceAllocationConsultationPerformance(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    ascription_department = models.CharField(max_length=100, verbose_name='归属科室', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    job_number = models.CharField(max_length=100, verbose_name='工号', blank=True, default="")
    doctor = models.CharField(max_length=100, verbose_name='医生', blank=True, default="")
    quantity = models.CharField(max_length=100, verbose_name='数量', blank=True, default="")
    performance_amount = models.CharField(max_length=100, verbose_name='数量', blank=True, default="")
    node = models.CharField(max_length=255, verbose_name='备注', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 夜班绩效
class PerformanceAllocationNightShiftPerformance(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_category = models.CharField(max_length=100, verbose_name='科室类别', blank=True, default="")
    ascription_department = models.CharField(max_length=100, verbose_name='归属科室', blank=True, default="")
    ascription_dep_code = models.CharField(max_length=100, verbose_name='归属科室编号', blank=True, default="")
    department = models.CharField(max_length=100, verbose_name='科室', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    fixed_number = models.CharField(max_length=100, verbose_name='每日固定人数', blank=True, default="")
    night_number = models.CharField(max_length=100, verbose_name='上夜班人数', blank=True, default="")
    night_standards = models.CharField(max_length=100, verbose_name='上夜班标准', blank=True, default="")
    end_night_number = models.CharField(max_length=100, verbose_name='下夜班人数', blank=True, default="")
    end_night_standards = models.CharField(max_length=100, verbose_name='下夜班标准', blank=True, default="")
    number_of_overnight_shifts = models.CharField(max_length=100, verbose_name='整夜班人数', blank=True, default="")
    full_night_shift_standard = models.CharField(max_length=100, verbose_name='整夜班标准', blank=True, default="")
    month_days = models.CharField(max_length=100, verbose_name='月天数', blank=True, default="")
    labor_dispatch = models.CharField(max_length=100, verbose_name='劳务派遣人员夜班', blank=True, default="")
    floating_fee = models.CharField(max_length=100, verbose_name='浮动夜班费', blank=True, default="")
    actual_occurrence = models.CharField(max_length=100, verbose_name='实发夜班绩效合计', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 科主任管理绩效
class PerformanceAllocationDirectorPerformance(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_name = models.CharField(max_length=100, verbose_name='科室名称', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    job_number = models.CharField(max_length=100, verbose_name='工号', blank=True, default="")
    name = models.CharField(max_length=100, verbose_name='姓名', blank=True, default="")
    position = models.CharField(max_length=100, verbose_name='行政职务', blank=True, default="")
    duration_employment = models.CharField(max_length=100, verbose_name='行政职务任职时间', blank=True, default="")
    department_category = models.CharField(max_length=100, verbose_name='科室类型', blank=True, default="")
    assessment_score = models.CharField(max_length=100, verbose_name='质量考核分数', blank=True, default="")
    management_standards = models.CharField(max_length=100, verbose_name='管理绩效标准', blank=True, default="")
    attendance_days = models.CharField(max_length=100, verbose_name='出勤天数', blank=True, default="")
    deduction_amount = models.CharField(max_length=100, verbose_name='年度考核扣罚金额', blank=True, default="")
    quarterly_assessment = models.CharField(max_length=100, verbose_name='季度考核扣罚金额', blank=True, default="")
    management_performance = models.CharField(max_length=100, verbose_name='管理绩效合计', blank=True, default="")
    assessment_coefficient = models.CharField(max_length=100, verbose_name='考核系数', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 综合目标绩效
class PerformanceAllocationComprehensiveTargetPerformance(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_type = models.CharField(max_length=10, verbose_name='科室类型', blank=True, default="")         # 1：临床科室， 2：医技及其他科室
    department_name = models.CharField(max_length=100, verbose_name='科室名称', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    people_number = models.CharField(max_length=100, verbose_name='人数', blank=True, default="")
    quality_dimension = models.CharField(max_length=100, verbose_name='医疗质量维度', blank=True, default="")
    nursing_quality = models.CharField(max_length=100, verbose_name='护理质量维度', blank=True, default="")
    medical_ethics = models.CharField(max_length=100, verbose_name='医德医风维度', blank=True, default="")
    medical_insurance = models.CharField(max_length=100, verbose_name='医保管理', blank=True, default="")
    research_teaching = models.CharField(max_length=100, verbose_name='科研教学', blank=True, default="")
    record_quality = models.CharField(max_length=100, verbose_name='病案质量', blank=True, default="")
    score = models.CharField(max_length=100, verbose_name='得分', blank=True, default="")
    target_performance = models.CharField(max_length=100, verbose_name='综合目标绩效', blank=True, default="")
    department_score = models.CharField(max_length=100, verbose_name='科主任考核得分', blank=True, default="")
    nurse_score = models.CharField(max_length=100, verbose_name='护士长考核得分', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

# 新入职人才储备绩效
class PerformanceAllocationNewTalentReserve(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="上传人", blank=True, null=True, on_delete=models.SET_NULL)
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")
    ascription_department = models.CharField(max_length=100, verbose_name='归属科室', blank=True, default="")
    djob_number = models.CharField(max_length=100, verbose_name='工号', blank=True, default="")
    name = models.CharField(max_length=100, verbose_name='姓名', blank=True, default="")
    title_level = models.CharField(max_length=100, verbose_name='职称级别', blank=True, default="")
    technical_title = models.CharField(max_length=100, verbose_name='技术职称', blank=True, default="")
    distribution_standards = models.CharField(max_length=255, verbose_name='发放标准（元）', blank=True, default="")
    due_this_month = models.CharField(max_length=100, verbose_name='本月应发（元）', blank=True, default="")
    leave_days = models.CharField(max_length=100, verbose_name='扣减天数', blank=True, default="")
    start_time = models.CharField(max_length=100, verbose_name='开始发放时间', blank=True, default="")
    end_time = models.CharField(max_length=100, verbose_name='终止时间', blank=True, default="")
    node = models.CharField(max_length=255, verbose_name='备注', blank=True, default="")
    working_days = models.CharField(max_length=100, verbose_name='上班天数', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")

class DepartmentPerformanceAllocation(BaseModel):
    assignor = models.ForeignKey('personal_center.User', verbose_name="分配人", blank=True, null=True, on_delete=models.SET_NULL)
    whether_allocate = models.CharField(max_length=20, verbose_name='是否已分配', default="0")           # 0：未分配，1：已分配
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")
    department_code = models.CharField(max_length=100, verbose_name='科室编号', blank=True, default="")

# 调班申请
class ShiftAdjustment(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '换班人'),
        ('two', '科主任'),
        ('three', '分管领导'),
        ('four', '分管部门'),
        ('five', '医院办公室'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )

    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL, related_name="ApplyUser")
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    telephone = models.CharField('申请人电话', max_length=128, blank=True, default="")
    old_duty_time = models.CharField('原值班时间', max_length=128, blank=True, default="")
    shift_change_personnel = models.ForeignKey('personal_center.User', verbose_name="换班人", blank=True, null=True, on_delete=models.SET_NULL, related_name="ShiftChangePersonnel")
    shift_change_telephone = models.CharField('换班人电话', max_length=128, blank=True, default="")
    shift_change_reason = models.CharField('换班原因', max_length=225, blank=True, default="")
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    can_revise = models.CharField('是否可以修改', max_length=50, default='1')
    note = models.CharField(verbose_name="备注", max_length=255, default='')
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='shift_adjustment_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='shift_adjustment_already_audit_users', blank=True)

    def get_state_str(self):
        if self.state == 'stop':
            return '终止'
        elif self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'

class ShiftAdjustmentRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '换班人'),
        ('two', '科主任'),
        ('three', '分管领导'),
        ('four', '分管部门'),
        ('five', '医院办公室')
    )
    parent_id = models.ForeignKey('ShiftAdjustment', verbose_name="换班申请", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

# 会议室请假
class MeetingRoomLeave(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人'),
        ('two', '分管部门'),
        ('three', '分管领导'),
        ('four', '医院办公室'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", blank=True, null=True, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='meeting_room_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='meeting_room_already_audit_users', blank=True)
    meet_main = models.ForeignKey('personal_center.ConferenceRoomApplication', verbose_name="会议室订阅", blank=False, null=True, on_delete=models.CASCADE)
    reason = models.CharField('请假理由', max_length=255, blank=True, default="")
    can_revise = models.CharField('是否可以修改', max_length=50, default='1')
    note = models.CharField(verbose_name="备注", max_length=255, default='')

    def get_state_str(self):
        if self.state == 'stop':
            return '终止'
        elif self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'

class MeetingRoomLeaveRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科室负责人'),
        ('two', '分管部门'),
        ('three', '分管领导'),
        ('four', '医院办公室')
    )
    parent_id = models.ForeignKey('MeetingRoomLeave', verbose_name="会议请假", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

# 科主任绩效分配
class PerformanceAllocationHead(BaseModel):
    apply_user = models.ForeignKey('personal_center.User', verbose_name="创建人", blank=True, null=True, on_delete=models.SET_NULL)
    state = models.CharField('是否提交', max_length=10, default='0')
    department_code = models.CharField('科室编号', max_length=100, blank=True, default="")
    department_name = models.CharField(max_length=100, verbose_name='科室名称', blank=True, default="")
    duties = models.CharField(max_length=50, verbose_name='职务', blank=True, default="")
    identity = models.CharField(max_length=50, verbose_name='医生/护士', blank=True, default="")
    job_number = models.CharField(max_length=100, verbose_name='工号', blank=True, default="")
    real_name = models.CharField(max_length=100, verbose_name='姓名', blank=True, default="")
    bank_account = models.CharField(max_length=225, verbose_name='银行账号', default='')
    workload = models.CharField(max_length=100, verbose_name='工作量绩效', blank=True, default="")
    night_shift_performance = models.CharField(max_length=100, verbose_name='夜班绩效', blank=True, default="")
    medical_community = models.CharField(max_length=100, verbose_name='医共体绩效', blank=True, default="")
    total_salary = models.CharField(max_length=100, verbose_name='绩效工资合计', blank=True, default="")
    managing_performance = models.CharField(max_length=100, verbose_name='科主任管理绩效', blank=True, default="")
    consultation_performance = models.CharField(max_length=100, verbose_name='会诊绩效', blank=True, default="")
    special_performance = models.CharField(max_length=100, verbose_name='门诊专项绩效', blank=True, default="")
    individual_performance = models.CharField(max_length=100, verbose_name='其他单项绩效', blank=True, default="")
    reserve_performance = models.CharField(max_length=100, verbose_name='人才储备绩效', blank=True, default="")
    years = models.CharField(max_length=100, verbose_name='年份', blank=True, default="")
    month = models.CharField(max_length=100, verbose_name='月份', blank=True, default="")